package com.wzt.lianfirecontrol.adapter.xiaokongshi;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.function.xiaokongshi.XksRecordModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DutyListItemAdapter extends BaseRecyclerAdapter<XksRecordModel> {

    private Context context;

    private boolean isHistory = false;

    public boolean isHistory() {
        return isHistory;
    }

    public void setHistory(boolean history) {
        isHistory = history;
    }

    public DutyListItemAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_duty_list_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, XksRecordModel data) {
        if (viewHolder instanceof MyViewHolder) {
            setItemData((MyViewHolder) viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView tv_duty_title;
        private TextView tv_one_more_day;
        private TextView tv_status;
        private View v_divider_line;

        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.tv_duty_title = itemView.findViewById(R.id.item_duty_title);
        myViewHolder.tv_one_more_day = itemView.findViewById(R.id.tv_one_more_day);
        myViewHolder.tv_status = itemView.findViewById(R.id.item_duty_status);
        myViewHolder.v_divider_line = itemView.findViewById(R.id.v_divider_line);
    }

    private void setItemData(MyViewHolder viewHolder, XksRecordModel data, int realPosition) {
        String startTime = data.getClassStartTimeStr();
        String endTime = data.getClassEndTimeStr();
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
            // 2020-06-14 06:00:00
            viewHolder.tv_duty_title.setText(startTime + "-" + endTime.split(" ")[1]);
            if (startTime.startsWith(endTime.split(" ")[0])){
                viewHolder.tv_one_more_day.setText("");
            }else {
                viewHolder.tv_one_more_day.setText("+1天");
            }
        }else {
            viewHolder.tv_duty_title.setText("");
            viewHolder.tv_one_more_day.setText("");
        }
        GradientDrawable gradientDrawable = new GradientDrawable();

            switch (data.getStatus()){//1：待值班；2：值班中；3：未值班；4：待交接；5：已完成
                case "1":
                    try {
                        String endStr = data.getClassEndTimeStr();// 2020-06-14 06:00
                        Date date = new Date();// 当前时间
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ConstData.TIME_FORMAT_YYYY_MM_DD_HH_MM);
                        Date endDate = simpleDateFormat.parse(endStr);
                        if (date.after(endDate)) {
                            data.setStatus("3");
                            viewHolder.tv_status.setText("未值班");
                            viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.gray));
                        } else {
                            viewHolder.tv_status.setText("待值班");
                            viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.dai_color));
                        }
                    }catch (Exception e){}
                    break;
                case "2":
                    viewHolder.tv_status.setText("值班中");
                    viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.look_status_color));
                    break;
                case "3":
                    viewHolder.tv_status.setText("未值班");
                    viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.gray));
                    break;
                case "4":
                    viewHolder.tv_status.setText("待交接");
                    viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.change_status_color));
                    break;
                case "5":
                    viewHolder.tv_status.setText("已完成");
                    viewHolder.tv_status.setTextColor(context.getResources().getColor(R.color.main_color));
                    break;

        }

        gradientDrawable.setCornerRadius(Utils.dip2px(context, 10));
        viewHolder.tv_status.setBackground(gradientDrawable);

        if (realPosition == getmDatas().size() - 1){
            viewHolder.v_divider_line.setVisibility(View.GONE);
        }else {
            viewHolder.v_divider_line.setVisibility(View.VISIBLE);
        }
    }
}
